package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CashByCreditCardBean;
import cn.rongdao.jrkabao.util.BigDecimalUtil;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.NumberUtil;

/**
 * Created by jack on 2017/11/6
 */

public class SelectFeeUIComponent extends RelativeLayout {
    public SelectFeeUIComponent(Context context) {
        super(context);
    }

    public SelectFeeUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectFeeUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvFee;
    private TextView tvTag;
    private TextView tvChange;
    private View viewLine;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvFee = findViewById(R.id.isf_tv_fee);
        tvTag = findViewById(R.id.isf_tv_tag);
        tvChange = findViewById(R.id.isf_tv_change);
        viewLine = findViewById(R.id.isf_view_line);
    }

    public void setFeeContent(CashByCreditCardBean.PassWay passWay) {
        setEnabled(CommonUtil.isStatusSuccess(passWay.getAllow_click()));
        setDescContent(passWay.getFee(), getServiceChange(passWay.getFee(), passWay.getMoney()), passWay.getPer_fee(), CommonUtil.isStatusSuccess(passWay.getAllow_click()));
        tvTag.setText(FRString.valueOf(passWay.getDesc()));
        tvTag.setVisibility(FRString.isEmpty(passWay.getDesc()) ? View.GONE : View.VISIBLE);
    }

    public void setFeeContentAndDrawable(CashByCreditCardBean.PassWay passWay) {
        setFeeContent(passWay);
        setChangeDrawable(passWay.isSelect());
    }

    public void setDescContent(String fee, String serviceCharge, String perFee, boolean isAllowClick) {
        tvFee.setEnabled(isAllowClick);
        tvTag.setEnabled(isAllowClick);
        if (FRString.isEmpty(serviceCharge)) {
            serviceCharge = "0.00";
        }
        String content;
        if (FRString.isEmpty(perFee) || perFee.equals("0")) {
            if (serviceCharge.equals("0.00")) {
                content = FRCollection.appendAsString("费率", getFeePercent(fee), "，手续费"
                        , serviceCharge, "元", "");
            } else {
                content = FRCollection.appendAsString("费率", getFeePercent(fee), "，手续费"
                        , FRString.valueOf(BigDecimalUtil.add(Double.valueOf(serviceCharge), Double.valueOf(perFee))), "元", "");
            }
        } else {
            if (serviceCharge.equals("0.00")) {
                content = FRCollection.appendAsString("费率", getFeePercent(fee), "+", perFee, "元，手续费"
                        , serviceCharge, "元", "");
            } else {
                content = FRCollection.appendAsString("费率", getFeePercent(fee), "+", perFee, "元，手续费"
                        , FRString.valueOf(BigDecimalUtil.add(Double.valueOf(serviceCharge), Double.valueOf(perFee))), "元", "");
            }
        }
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(isAllowClick ? ContextCompat.getColor(getContext(), R.color.cff7048) : Color.parseColor("#7FFF7048")),
                content.lastIndexOf("费") + 1, content.length() - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvFee.setText(ss);
    }

    private String getFeePercent(String fee) {
        if (FRString.isEmpty(fee)) {
            return "0.00%";
        }
        return FRCollection.appendAsString(FRString.valueOf(BigDecimalUtil.mul(Double.valueOf(fee), 100.00)), "%", "");
    }

    public String getServiceChange(String fee, String money) {
        if (FRString.isEmpty(fee)) {
            return FRString.EMPTY_STRING;
        }
        if (FRString.isEmpty(money)) {
            return FRString.EMPTY_STRING;
        }
        double fe = Double.valueOf(fee);
        return NumberUtil.getTwoDecimalNumber(fe * Double.valueOf(money));
    }

    public void setViewLineVisible(boolean isVisible) {
        viewLine.setVisibility(isVisible ? VISIBLE : GONE);
    }

    private void setChangeDrawable(boolean isSelect) {
        tvChange.setCompoundDrawablesWithIntrinsicBounds(0, 0, isSelect ? R.drawable.icon_select_credit_card : 0, 0);
    }

    public void setChangeContent(OnClickListener clickListener) {
        tvChange.setText("更换");
        tvChange.setOnClickListener(clickListener);
    }
}